/**
 * Eclipse Outliner
 * Copyright (C) 2006 Jeff Mesnil
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.jmesnil.opml.core.internal;

import java.io.ByteArrayInputStream;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.jmesnil.opml.core.IBody;
import net.jmesnil.opml.core.IHead;
import net.jmesnil.opml.core.IOPML;
import net.jmesnil.opml.core.IOutline;
import net.jmesnil.opml.core.IOutlineNode;
import net.jmesnil.opml.core.IOwner;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.helpers.DefaultHandler;

public class OPMLBuilder extends DefaultHandler {

    private Date creation = new Date();

    public IOPML create(String xml) throws Exception {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document document = builder.parse(new ByteArrayInputStream(xml
                .getBytes()));
        IHead head = createHead(document.getElementsByTagName("head").item(0));
        IBody body = createBody(document.getElementsByTagName("body").item(0));
        return new OPML(head, body);
    }

    private IHead createHead(Node node) {
        NodeList childNodes = node.getChildNodes();
        String title = "";
        String ownerName = "";
        String ownerId = "";
        String ownerEmail = "";
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if ("title".equals(child.getNodeName())) {
                    title = child.getFirstChild().getNodeValue();
                }
                if ("ownerName".equals(child.getNodeName())) {
                    Node n = child.getFirstChild();
                    ownerName = (n != null) ? n.getNodeValue() : "";
                }
                if ("ownerId".equals(child.getNodeName())) {
                    Node n = child.getFirstChild();
                    ownerId = (n != null) ? n.getNodeValue() : "";
                }
                if ("ownerEmail".equals(child.getNodeName())) {
                    Node n = child.getFirstChild();
                    ownerId = (n != null) ? n.getNodeValue() : "";
                }
            }
        }
        IOwner owner = new Owner(ownerId, ownerName, ownerEmail);
        return new Head(title, owner, creation, creation);
    }

    private IBody createBody(Node node) {
        IBody body = new Body();
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if ("outline".equals(child.getNodeName())) {
                    body.appendChild(createOutline(child, body));
                }
            }
        }
        return body;
    }

    private IOutline createOutline(Node outlineNode, IOutlineNode parent) {
        String text = outlineNode.getAttributes().getNamedItem("text")
                .getNodeValue();
        IOutline outline = new Outline(parent, text, creation);
        
        NodeList childNodes = outlineNode.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if ("outline".equals(child.getNodeName())) {
                    outline.appendChild(createOutline(child, outline));
                }
            }
        }
        return outline;
    }

}
